﻿
using Admin.NET.Core;
using Admin.NET.Core.Btms;
using DocumentFormat.OpenXml.Office.CustomUI;

namespace Admin.NET.Application.Service;
public partial class TraShenQingService
{
    /// <summary>
    /// 配发血申请单查询列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<SqlSugarPagedList<TraShenQingPageOutput>> PeiFaXuePage(TraShenQingPeiFaXuePageInput input)
    {
        var dateArray = new List<DateTime>();
        if (input.riQiGuoLv != null)
        {
            foreach(var item in input.riQiGuoLv)
            {
                dateArray.Add(DateTime.Now.AddDays(item).Date);
            }
        }
        bool yiPeiXue = input.peiXueZhuangTai != null && input.peiXueZhuangTai.IndexOf("已配血") > -1 && input.peiXueZhuangTai.IndexOf("未配血") == -1;
        bool weiPeiXue = input.peiXueZhuangTai != null && input.peiXueZhuangTai.IndexOf("未配血") > -1 && input.peiXueZhuangTai.IndexOf("已配血") == -1;

        var traShenQingOutputList = await _repTraShenQingHuanZhe.Queryable
            .InnerJoin<TraShenQing>((huanZhe, zhuBiao) => huanZhe.Id == zhuBiao.Id)
            .WhereIF(input.danHao.HasValue(), (huanZhe, zhuBiao) => zhuBiao.Id == input.danHao)
            // 只查询已生效的单据
            .Where((huanZhe, zhuBiao) => zhuBiao.danJuZhuangTai == DanJuZhuangTai.已审核)
            .InnerJoin<TraShenQingYongXue>((huanZhe, zhuBiao, yongXue) => huanZhe.Id == yongXue.Id)
            .LeftJoin<BasXueYePinZhong>((huanZhe, zhuBiao, yongXue, pinZhong) => yongXue.shenQingPinZhong == pinZhong.Id)
           .WhereIF(dateArray.Count > 0, (huanZhe, zhuBiao, yongXue, pinZhong) => dateArray.Contains(zhuBiao.CreateTime.Date))
           // 涉及多个表的模糊查询，FuzzyQuery 不满足，先这样子写
           .WhereIF(input.Keyword.HasValue(), (huanZhe, zhuBiao, yongXue, pinZhong) => huanZhe.huanZheId.Contains(input.Keyword) || huanZhe.zhuYuanHao.Contains(input.Keyword) 
           || huanZhe.xingMing.Contains(input.Keyword) || huanZhe.bingQu.Contains(input.Keyword)
           || yongXue.yongXueLeiXing.Contains(input.Keyword) || yongXue.shuXueMuDi.Contains(input.Keyword)
           || pinZhong.mingCheng.Contains(input.Keyword))
           .LeftJoin<TraPeiXue>((huanZhe, zhuBiao, yongXue, pinZhong, peiXue) => zhuBiao.Id == peiXue.traShenQingId)
            .WhereIF(yiPeiXue, (huanZhe, zhuBiao, yongXue, pinZhong, peiXue) => peiXue.Id != null && peiXue.danJuZhuangTai == DanJuZhuangTai.已生效)
            .WhereIF(weiPeiXue, (huanZhe, zhuBiao, yongXue, pinZhong, peiXue) => peiXue.Id == null || peiXue.danJuZhuangTai == DanJuZhuangTai.已作废)
           .Select<TraShenQingPageOutput>()
           .OrderByDescending(huanZhe => huanZhe.Id)
           .ToPagedListAsync(input.Page, input.PageSize);

        return traShenQingOutputList;
    }
    /// <summary>
    /// 获取申请单配发血信息
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<PeiFaXueInfoOutput> GetPeiFaXueInfo(string id)
    {
        // 复核单
        var fuHe = await _traFuHe.Queryable.Where(t => t.Id == id).Select<TraFuHeOutput>().OrderBy(t => t.createTime).FirstAsync();
        // 配血单
        var peiXueList = await _traPeiXue.Queryable.Where(t => t.traShenQingId == id).Select<TraPeiXueOutput>().OrderBy(t => t.createTime).ToListAsync();
        // 发血单
        var faXueList = await _traFaXue.Queryable.Where(t => t.traShenQingId == id).Select<TraFaXueOutput>().OrderBy(t => t.createTime).ToListAsync();

        return new PeiFaXueInfoOutput
        {
            fuHe = fuHe,
            peiXueList = peiXueList,
            faXueList = faXueList
        };
    }
    /// <summary>
    /// 获取患者历史输血、历史复核、历史配血、历史输血反应次数
    /// TODO: 历史输血反应次数未实现
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public CheckHuanZheLiShiCiShuOutput CheckHuanZheLiShiCiShu(CheckHuanZheLiShiCiShuInput input)
    {
        // 历史输血次数
        var shuXueCiShu = _repTraShenQingHuanZhe.Queryable.Where(huanZhe => input.huanZheId.HasValue() && huanZhe.huanZheId == input.huanZheId 
                        || input.zhuYuanHao.HasValue() && huanZhe.zhuYuanHao == input.zhuYuanHao)
                        .InnerJoin<TraFaXue>((huanZhe, faXue) => huanZhe.Id == faXue.traShenQingId)
                        .Where((huanZhe, faXue) => faXue.danJuZhuangTai == DanJuZhuangTai.已生效)
                        .Count();
        // 历史复核次数
        var fuHeCiShu = _traFuHe.Queryable.Where(fuHe => fuHe.danJuZhuangTai == DanJuZhuangTai.已生效)
                        .InnerJoin<TraShenQingHuanZhe>((fuHe, huanZhe) => fuHe.Id == huanZhe.Id)
                        .Where((fuHe, huanZhe) => input.huanZheId.HasValue() && huanZhe.huanZheId == input.huanZheId
                        || input.zhuYuanHao.HasValue() && huanZhe.zhuYuanHao == input.zhuYuanHao)
                        .Count();
        // 历史配血次数
        var peiXueCiShu = _traPeiXue.Queryable.Where(peiXue => peiXue.danJuZhuangTai == DanJuZhuangTai.已生效)
                        .InnerJoin<TraShenQingHuanZhe>((peiXue, huanZhe) => peiXue.traShenQingId == huanZhe.Id)
                        .Where((peiXue, huanZhe) => input.huanZheId.HasValue() && huanZhe.huanZheId == input.huanZheId
                        || input.zhuYuanHao.HasValue() && huanZhe.zhuYuanHao == input.zhuYuanHao)
                        .Count();

        return new CheckHuanZheLiShiCiShuOutput
        {
            liShiShuXueCiShu = shuXueCiShu,
            liShiFuHeCiShu = fuHeCiShu,
            liShiPeiXueCiShu = peiXueCiShu,
            liShiShuXueFanYingCiShu = 0
        };

    }
    /// <summary>
    /// 获取患者最新的输血史、反应史、孕产史、移植史等信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public CheckHuanZheLatestInfoOutput CheckHuanZheLatestInfo(CheckHuanZheLiShiCiShuInput input)
    {
        // 获取患者上一张申请单的输血史、反应史、孕产史、移植史
        var entitiy = _repTraShenQingHuanZhe.Queryable
                        .Where(t => input.huanZheId.HasValue() && t.huanZheId == input.huanZheId || input.zhuYuanHao.HasValue() && t.zhuYuanHao == input.zhuYuanHao)
                        .InnerJoin<TraShenQing>((huanZhe, shenQing) => huanZhe.Id == shenQing.Id)
                        .Where((huanZhe, shenQing) => shenQing.danJuZhuangTai != DanJuZhuangTai.已作废)
                        .OrderByDescending((huanZhe, shenQing) => shenQing.Id)
                        .Select<CheckHuanZheLatestInfoOutput>()
                        .First();
        // 获取患者最新的血型复核结果
        var fuHeEntity = _traFuHe.Queryable.Where(fuHe => fuHe.danJuZhuangTai == DanJuZhuangTai.已生效)
                        .InnerJoin<TraShenQingHuanZhe>((fuHe, huanZhe) => fuHe.Id == huanZhe.Id)
                        .Where((fuHe, huanZhe) => input.huanZheId.HasValue() && huanZhe.huanZheId == input.huanZheId
                        || input.zhuYuanHao.HasValue() && huanZhe.zhuYuanHao == input.zhuYuanHao)
                        .OrderByDescending((fuHe, huanZhe) => fuHe.CreateTime)
                        .Select((fuHe, huanZhe) => new { 
                            fuHeABO = fuHe.aboFuHeJieGuo,
                            fuHeRhD = fuHe.rhDFuHeJieGuo,
                            kangShaiJieGuo = fuHe.kangShaiJieGuo
                        }).First();
        if (fuHeEntity != null)
        {
            entitiy.fuHeABO = fuHeEntity.fuHeABO;
            entitiy.fuHeRhD = fuHeEntity.fuHeRhD;
            entitiy.kangShaiJieGuo = fuHeEntity.kangShaiJieGuo;
        }

        return entitiy;
    }

    /// <summary>
    /// 获取患者的历史复核分页列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<SqlSugarPagedList<HuanZheLiShiFuHeOutput>> CheckLiShiFuHePage(CheckLiShiPageInput input)
    {
        var list = await _traFuHe.Queryable.Where(fuHe => fuHe.danJuZhuangTai == DanJuZhuangTai.已生效)
                   .InnerJoin<TraShenQingHuanZhe>((fuHe, huanZhe) => fuHe.Id == huanZhe.Id)
                   .Where((fuHe, huanZhe) => input.huanZheId.HasValue() && huanZhe.huanZheId == input.huanZheId
                        || input.zhuYuanHao.HasValue() && huanZhe.zhuYuanHao == input.zhuYuanHao)
                   .OrderByDescending((fuHe, huanZhe) => fuHe.CreateTime)
                   .Select<HuanZheLiShiFuHeOutput>()
                   .ToPagedListAsync(input.page, input.pageSize);

        return list;
    }
    /// <summary>
    /// 获取患者的历史输血分页列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<SqlSugarPagedList<HuanZheLiShiFaXueOutput>> CheckLiShiShuXuePage(CheckLiShiPageInput input)
    {
        var list = await _traFaXue.Queryable.Where(faXue => faXue.danJuZhuangTai == DanJuZhuangTai.已生效)
                   .InnerJoin<TraShenQingHuanZhe>((faXue, huanZhe) => faXue.traShenQingId == huanZhe.Id)
                   .Where((faXue, huanZhe) => input.huanZheId.HasValue() && huanZhe.huanZheId == input.huanZheId
                        || input.zhuYuanHao.HasValue() && huanZhe.zhuYuanHao == input.zhuYuanHao)
                   .InnerJoin<TraShenQingYongXue>((faXue, huanZhe, yongXue) => huanZhe.Id == yongXue.Id)
                   .InnerJoin<BasXueYePinZhong>((faXue, huanZhe, yongXue, pinZhong) => yongXue.shenQingPinZhong == pinZhong.Id)
                   .OrderByDescending((faXue, huanZhe, yongXue, pinZhong) => faXue.CreateTime)
                   .Select<HuanZheLiShiFaXueOutput>()
                   .ToPagedListAsync(input.page, input.pageSize);

        // 分页后获取发血品种明细 
        var faXueIdList = list.Items.Select(t => t.faXueId).ToList();
        var faXueMingXiList = await _traFaXueMingXi.Queryable.Where(t => faXueIdList.Contains(t.traFaXueId))
                            .InnerJoin<InvKuCun>((faXueMingXi, xueYe) => faXueMingXi.xueYeId == xueYe.Id)
                            .InnerJoin<BasXueYePinZhong>((faXueMingXi, xueYe, pinZhong) => xueYe.pinZhongId == pinZhong.Id)
                            .Select((faXueMingXi, xueYe, pinZhong) => new
                            {
                                pinZhongMingCheng = pinZhong.mingCheng,
                                danWei = pinZhong.danWei,
                                rongLiang = xueYe.rongLiang,
                                faXueId = faXueMingXi.traFaXueId
                            }).ToListAsync();
        foreach(var item in list.Items)
        {
            // 将品种做分组，分组后sum容量
            var faXueMingXiHuiZong = faXueMingXiList.Where(t => t.faXueId == item.faXueId).GroupBy(t => new { t.pinZhongMingCheng, t.danWei })
                                    // 拼接品种汇总字符串，容量用ToString("G29")只显示有效数字
                                    .Select(t => t.Key.pinZhongMingCheng + t.Sum(s => s.rongLiang).ToString("G29") + t.Key.danWei).ToList();
            item.faXueHuiZong = string.Join(",", faXueMingXiHuiZong);
            // 获取总的发血量
            item.faXueLiang = faXueMingXiList.Where(t => t.faXueId == item.faXueId).Sum(t => t.rongLiang);

        }
        return list;
    }
    /// <summary>
    /// 获取患者历史配血分页列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<SqlSugarPagedList<HuanZheLiShiPeiXueOutput>> CheckLiShiPeiXuePage(CheckLiShiPageInput input)
    {
        var list = await _traPeiXue.Queryable.Where(peiXue => peiXue.danJuZhuangTai == DanJuZhuangTai.已生效)
                     .InnerJoin<TraShenQingHuanZhe>((peiXue, huanZhe) => peiXue.traShenQingId == huanZhe.Id)
                     .Where((peiXue, huanZhe) => input.huanZheId.HasValue() && huanZhe.huanZheId == input.huanZheId
                        || input.zhuYuanHao.HasValue() && huanZhe.zhuYuanHao == input.zhuYuanHao)
                   .InnerJoin<TraShenQingYongXue>((peiXue, huanZhe, yongXue) => huanZhe.Id == yongXue.Id)
                   .OrderByDescending((peiXue, huanZhe, yongXue) => peiXue.CreateTime)
                   .Select<HuanZheLiShiPeiXueOutput>()
                   .ToPagedListAsync(input.page, input.pageSize);
        // 获取配血明细
        var peiXueIdList = list.Items.Select(t => t.traPeiXueId).ToList();
        var peiXueMingXiList = await _traPeiXueMingXi.Queryable.Where(peiXueMingXi => peiXueIdList.Contains(peiXueMingXi.traPeiXueId))
                               .InnerJoin<InvKuCun>((peiXueMingXi, xueYe) => peiXueMingXi.xueYeId == xueYe.Id)
                               .Select((peiXueMingXi, xueYe) => new
                               {
                                   peiXueMingXi.traPeiXueId,
                                   xueYe.xianXueMa,
                                   xueYe.chanPinId,
                                   xueYe.mingCheng,
                                   xueYe.rongLiang,
                                   xueYe.danWei,
                                   peiXueMingXi.peiXueJieGuo
                               }).ToListAsync();
        foreach(var item in list.Items)
        {
            // 拼接配血信息，多袋血合并一起显示
            var mingXiList = peiXueMingXiList.Where(t => t.traPeiXueId == item.traPeiXueId).Select(t =>
             "" + t.xianXueMa + " | " + t.chanPinId + " | " + t.mingCheng + " | " + t.rongLiang.ToString("G29") + t.danWei + " -- " + t.peiXueJieGuo
            ).ToList();
            item.peiXueMingXiHuiZong = string.Join("<br >", mingXiList);
        }
        return list;
    }
}
